package com.medicine.hbp.ui;

import java.util.List;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.sp.DadosSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;

/**
 * 热点
 * @author jindandan
 */
public class HotspotActivity extends BaseActivity implements IXListViewListener {
	private XListView listView;
	private ClassificationDetailAdapter adapter;
	private ImageView image_no_wifi;

	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	private List<artGroupVo> artGroupVos;
	private String name;
	private String id;
	private int type;//1 热点  2专题


	@Override
	public void setRootView() {
		super.setRootView();
		name=getIntent().getStringExtra("name");
		id=getIntent().getStringExtra("id");
		type=getIntent().getIntExtra("type", 1);
		setContentView(R.layout.activity_hotspot);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initTop();
		listView = (XListView) findViewById(R.id.xListView_detail);
		image_no_wifi = (ImageView) findViewById(R.id.image_no_wifi);
		adapter = new ClassificationDetailAdapter(aty);
		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		adapter.notifyDataSetChanged();
		image_no_wifi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(aty)==0){
					listView.setVisibility(View.GONE);
					image_no_wifi.setVisibility(View.VISIBLE);
					ToastUtil.showShort(aty, "还没有网络哦");
				}else{
					listView.setVisibility(View.VISIBLE);
					image_no_wifi.setVisibility(View.GONE);
					httpArtGroupList();
				}
			}
		});
		listView.setVisibility(View.VISIBLE);
		image_no_wifi.setVisibility(View.GONE);
		httpArtGroupList();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle(name);
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		topview.setRightVisibility(View.INVISIBLE);
	}

	/**
	 * 标签内部文章组获取
	 */
	private void httpArtGroupList() {
		String jk;
		if(type==1){
			jk=HttpInterface.GET_LABER_HOTBYPOSITION;
		}else{
			jk=HttpInterface.GET_LABER_PROJECT_BY_POSITION;
		}
		HttpParams params = new HttpParams();
		params.put("positionNumber", id);
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(this,HttpInterface.GET_LABER_HOTBYPOSITION, params,
				new AppHttpCallBack<artGroupListVo>(artGroupListVo.class, aty) {
					@Override
					public void onSuccess(artGroupListVo result) {
						if(result!=null){
							boolean hasMore = true;
							artGroupVos = result.artInfo;
							if (refresh) {
								adapter.changeData(result.artInfo);
							} else {
								adapter.addItems(result.artInfo);
								adapter.notifyDataSetChanged();
							}
							if (null!=result.artInfo&&result.artInfo.size() < pageSize) {
								hasMore = false;
							}
							listView.setPullLoadEnable(hasMore);
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						listView.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});

	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		listView.setPullLoadEnable(false);
		httpArtGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpArtGroupList();
	}
}
